package com.course.design.controller;

import cn.hutool.json.JSONObject;
import com.course.design.common.Result;
import com.course.design.constant.SystemConstant;
import com.course.design.constants.Constant;
import com.course.design.entity.Ygxx;
import com.course.design.entity.vo.UserLoginVO;
import com.course.design.entity.vo.YgxxQueryVo;
import com.course.design.service.YgxxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 描述:
 *
 * @author qinlianji
 * @create 2020-02-06
 */
@Api(tags = {"员工信息"})
@RestController
@RequestMapping("/ygxx")
public class YgxxController {

    @Autowired
    private YgxxService ygxxService;

    @ApiOperation(value = "新增或更新员工信息")
    @PostMapping("/saveOrUpdate")
    public Result save(@RequestBody Ygxx ygxx) {
        ygxxService.saveOrUpdate(ygxx);
        return Result.success(ygxx);
    }

    @ApiOperation(value = "注册申请")
    @PostMapping("/register")
    public Result register(@RequestBody Ygxx ygxx) {
        ygxx.setSjyxbjFg(SystemConstant.SQSP);
        ygxxService.saveOrUpdate(ygxx);
        return Result.success(ygxx);
    }

    @ApiOperation(value = "员工更改自己的信息")
    @PostMapping("/ygUpdateSelf")
    public Result ygUpdateSelf(@RequestBody Ygxx ygxx) {
        return Result.success(ygxxService.ygUpdateSelf(ygxx));
    }

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public Result save(@RequestBody UserLoginVO vo) {
        Ygxx login = ygxxService.login(vo);
        if (ObjectUtils.isEmpty(login)) {
            return Result.fail("用户名不存在或密码不正确！");
        }
        return Result.success(login);
    }

    @ApiOperation(value = "审批员工通过")
    @GetMapping("/approval/{ygshId}")
    public Result approval(@PathVariable(name = "ygshId") String ygshId) {
        ygxxService.updateSjyxbj(ygshId);
        return Result.success();
    }


    @ApiOperation(value = "删除员工 逻辑删除  只对有效数据有效")
    @GetMapping("/delete/{ygshId}")
    public Result delete(@PathVariable(name = "ygshId") String ygshId) {
        ygxxService.deleteById(ygshId);
        return Result.success();
    }

    @ApiOperation(value = "删除员工 真删除  只对有效数据有效")
    @GetMapping("/deleteReal/{ygshId}")
    public Result deleteReal(@PathVariable(name = "ygshId") String ygshId) {
        ygxxService.deleteReal(ygshId);
        return Result.success();
    }

    @ApiOperation(value = "批量删除员工")
    @PostMapping("/batchDelete")
    public Result batchDelete(@RequestBody JSONObject json) {
        ygxxService.batchDeleteById((String)json.get("ids"));
        return Result.success();
    }

    @ApiOperation(value = "根据唯一标示查询员工")
    @GetMapping("/query/{ygshId}")
    public Result query(@PathVariable(name = "ygshId") String ygshId) {
        return Result.success(ygxxService.queryById(ygshId));
    }

    @ApiOperation(value = "重置密码")
    @PostMapping("/resetPassword/{ygshId}")
    public Result resetPassword(@PathVariable("ygshId") String ygshId) {
        if (ygxxService.changePassword(Constant.DEFAULT_PASSWORD, ygshId)) {
            return Result.success();
        }
        return Result.fail("更改失败");
    }

    @ApiOperation(value = "查询联系人")
    @GetMapping("/queryFriends/{userId}")
    public Result queryFriends(@PathVariable("userId") String userId){
        return Result.success(ygxxService.queryFriends(userId));
    }

    @ApiOperation(value = "查询本部门联系人")
    @GetMapping("/queryDeptFriends/{userId}")
    public Result queryDeptFriends(@PathVariable("userId") String userId){
        return Result.success(ygxxService.queryDeptFriends(userId));
    }

    @ApiOperation(value = "分页查询所有联系人信息")
    @GetMapping("/queryPage")
    public Result queryPage(int start, int limit, String username){
        return Result.success(ygxxService.queryPage(start, limit, username));
    }

    @ApiOperation(value = "通过部门id查询联系人信息")
    @GetMapping("/queryByDeptdmCd")
    public Result queryByDeptdmCd(String deptdmCd){
        return Result.success(ygxxService.queryByDeptdmCd(deptdmCd));
    }

    @ApiOperation(value = "分页查询所有申请联系人信息")
    @GetMapping("/querySqPage")
    public Result querySqPage(int start, int limit, String username){
        return Result.success(ygxxService.querySqPage(start, limit, username));
    }
}
